﻿Imports System.Drawing

Public Class Message
    Implements IDisposable

    Private mViewEx As FMessage
    Private mInitialMessage As String
    Private mLeftIconSet() As System.Drawing.Image
    Private mRightIconSet() As System.Drawing.Image

    Public Sub New()
        Dim aIcons() As Image = {My.Resources.Buddy.ToBitmap}
        NewWorker(String.Empty, aIcons, aIcons)
    End Sub

    Public Sub New(ByVal initialMessage As String)
        Dim aIcons() As Image = {My.Resources.Buddy.ToBitmap}
        NewWorker(initialMessage, aIcons, aIcons)
    End Sub
    Public Sub New(ByVal initialMessage As String, ByVal rightIconSet() As System.Drawing.Image)
        Dim aIcons() As Image = {My.Resources.Buddy.ToBitmap}
        NewWorker(initialMessage, aIcons, rightIconSet)
    End Sub
    Public Sub New(ByVal initialMessage As String, ByVal leftIconSet() As System.Drawing.Image, ByVal rightIconSet() As System.Drawing.Image)
        NewWorker(initialMessage, leftIconSet, rightIconSet)
    End Sub
    Private Sub NewWorker(ByVal initialMessage As String, ByVal leftIconSet() As System.Drawing.Image, ByVal rightIconSet() As System.Drawing.Image)
        mInitialMessage = initialMessage
        mLeftIconSet = leftIconSet
        mRightIconSet = rightIconSet

        CreateWindowOnNewThread()
    End Sub

    Private mMessageFrozen As Boolean = False
    Public Sub FreezeMessage()
        mMessageFrozen = True
    End Sub

    Private Sub CreateWindowOnNewThread()
        Dim clsMRE As New System.Threading.ManualResetEvent(False)

        Dim clsThread As New System.Threading.Thread(AddressOf CreateWindow)
        clsThread.IsBackground = True
        clsThread.Name = "Message Window Thread"
        clsThread.Priority = System.Threading.ThreadPriority.AboveNormal
        clsThread.Start(clsMRE)

        clsMRE.WaitOne(100000, True)
    End Sub
    Private Sub CreateWindow(ByVal state As Object)
        Dim clsMRE As System.Threading.ManualResetEvent = CType(state, System.Threading.ManualResetEvent)

        mViewEx = New FMessage
        mViewEx.Show(mInitialMessage, mLeftIconSet, mRightIconSet)
        clsMRE.Set()
        System.Windows.Forms.Application.Run(mViewEx)
    End Sub

    Public Sub SetMessage(ByVal text As String)
        If mMessageFrozen Then Return

        If mViewEx.InvokeRequired Then
            Dim clsD As New Action(Of String)(AddressOf mViewEx.SetMessage)
            mViewEx.Invoke(clsD, New Object() {text})
        Else
            mViewEx.SetMessage(text)
        End If
    End Sub
    Public Sub SetRightIconSet(ByVal icons() As System.Drawing.Image)
        If mViewEx.InvokeRequired Then
            Dim clsD As New Action(Of System.Drawing.Image())(AddressOf mViewEx.SetRightIconSet)
            mViewEx.Invoke(clsD, New Object() {icons})
        Else
            mViewEx.SetRightIconSet(icons)
        End If
    End Sub


    Public Sub Close()
        If Not mViewEx Is Nothing Then
            If mViewEx.InvokeRequired Then
                Dim clsD As New Action(AddressOf mViewEx.Close)
                mViewEx.Invoke(clsD)
            Else
                mViewEx.Close()
            End If
        End If
    End Sub

    Private Sub Dispose() Implements System.IDisposable.Dispose
        Me.Close()
    End Sub

    Public ReadOnly Property Form() As Form
        Get
            Return mViewEx
        End Get
    End Property

End Class
